//
//  GameOverScene.m
//  MyCocos2DSimpleGame
//
//  Created by How Charles on 11-4-22.
//  Copyright 2011 hust. All rights reserved.
//

#import "GameOverScene.h"

#import "GameMusicSetting.h"
#import "Particle.h"
#import "MenuTest.h"
#import "GameScene.h"

@implementation GameOverScene
@synthesize layer=_layer;

#if 1
///从1开始
int score_min[11]=
{   100,
	100,300,500,700,1000,
	1500,1800,2000,2200,2500	
};
#else
int score_min[11]=
{   100,
	100,200,300,400,500,
	600,700,800,900,1000	
};
#endif

-(id)init
{
	if(self =[super init])
	{
		
		self.layer=[GameOverLayer node];
		[self addChild:_layer];
	}
	
	return self;
}

-(void)dealloc
{
	[_layer release];
	_layer=nil;
	
	[super dealloc];
}


-(GameOverLayer *)getLayer
{
	return self.layer;
}


@end




@implementation GameOverLayer

@synthesize label=_label;
@synthesize goal_label;

-(id)init
{
	if(self=[super init])
	{
		NSLog(@"----------self.isTouchEnabled==========%d------",self.isTouchEnabled);
		self.isTouchEnabled=true;
		
		
		
		/*
		[self runAction:
		 [CCSequence actions:
		  [CCDelayTime actionWithDuration:3],
		  [CCCallFunc actionWithTarget:self 
							  selector:@selector(gameOverDone)],
		  nil
		  ]
		 ];
		 */
        [GameMusicSetting pauseBackgroundMusic];
		 [GameMusicSetting playEffect:@"levelfailed.mp3"];
	}
	
	return self; 
		
}






-(NSString *)getScoreString
{
	return [NSString stringWithFormat:@"%d",[self getScore]];
}


-(int)getScore
{
	return play_score;
}

-(int)setScore:(int)score
{
	play_score=score;
}

-(void)setNewHighScore:(bool)isNewHigh
{
	bIsNewHightScore=isNewHigh;
	
	if(bIsNewHightScore)
	{
		[self displayNewHighScoreAnim:self];
		
		CGSize winSize=[[CCDirector sharedDirector] winSize];
		[Particle playParticle:self name:@"lavaFlow.plist" pos:ccp(winSize.width/2,winSize.height/7*6)];
	}
}


-(void)displayNewHighScoreAnim:(id)sender
{
	CGSize size=[[CCDirector sharedDirector] winSize];
	
	CCLabel * label=[CCLabel labelWithString:@"New High Score" 
										  fontName:@"Marker Felt"
										  fontSize:60];
	label.color=ccc3(255, 0, 0);
	label.position=ccp(size.width/2,size.height/2);
	[sender addChild:label z:7 tag:0];
	
	////////定义动作
	CCMoveBy * moveBy=[CCMoveBy actionWithDuration:4 position:ccp(0,size.height)];
	//CCMoveBy * moveBy2=[CCMoveBy actionWithDuration:0.5 position:ccp(0,((winSize.height)/16))];
	CCFadeOut * fade=[CCFadeOut actionWithDuration:4];
	//CCFadeIn * fade2=[CCFadeIn actionWithDuration:0.5];
	
	CCSpawn * together=[CCSpawn actions:moveBy,fade,nil];
	
	CCCallFuncN * call=[CCCallFuncN actionWithTarget:self selector:@selector(removeSprite:)];
	
	CCSequence * seq=[CCSequence actions:together,call,nil];
	
	[label runAction:seq];
}

-(void)removeSprite:(id)sender
{
	CCSprite * sprite=(CCSprite *)sender;
	
	[sender removeChild:sprite cleanup:YES];
}



-(bool)getNewHighScore
{
	return bIsNewHightScore;
}

-(void)setResult
{

	
	CGSize winSize = [[CCDirector sharedDirector] winSize];
	int w=winSize.width;
	int h=winSize.height;
	
	//游戏结束的提示信息
	//背景
    
    NSString * bg_png=nil;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        bg_png=@"Background_doodle-hd.png";
    }
    else{
        bg_png=@"Background_doodle.png";
    }
	
	CCSprite *spritePopup = [CCSprite spriteWithFile:bg_png];
	//spriterightBAR.position = ccp(400,50);
	spritePopup.position = ccp(w/2,h/2);
	//spritePopup.opacity=90;
	[self addChild:spritePopup ];
	

	
	///分数信息
	/*
	NSString * str=[NSString stringWithFormat:@"Goal: $%d",score_min[i_current_level]];
	goal_label=[CCLabelTTF labelWithString:str 
										   fontName:@"Arial"
										   fontSize:25];
	goal_label.color=ccc3(0, 0, 0);
	goal_label.position=CGPointMake(w/2, 
								h/2+60);
	[self addChild:goal_label ];
	*/
	
	
	NSString * str_font_name=[NSString stringWithFormat:@"cMode_number_b-hd.png"];
					
	CCSprite * tmp=[CCSprite spriteWithFile:str_font_name];
	int w_txt=tmp.contentSize.width/10;
	int h_txt=tmp.contentSize.height;
	
	self.label=[CCLabelAtlas labelAtlasWithString:[self getScoreString]
						  charMapFile:str_font_name
									 itemWidth:w_txt itemHeight:h_txt startCharMap:'0'];
		

	_label.position=CGPointMake(winSize.width/2, 
								winSize.height/2);
	_label.anchorPoint=ccp(0.5,0.5);
	[self addChild:_label];
	
	
	//操作菜单
	CCMenuItemImage* replay = [CCMenuItemImage itemFromNormalImage:@"button_ingame_retry@1x.png" 
													 selectedImage:@"button_ingame_retry_lit@1x.png" 
															target:self selector:@selector(replayGame:)];
	

	 
	
	///退出
	CCMenuItem * quit_button = [CCMenuItemImage 
								itemFromNormalImage:@"button_ingame_quit@1x.png" 
								selectedImage:@"button_ingame_quit_lit@1x.png" 
								target:self 
								selector:@selector(quitCallback:)];
	
	////button
	CCMenu * cmn;
	cmn =[CCMenu menuWithItems:replay,quit_button,nil];
	
	[cmn alignItemsHorizontally];
	
	cmn.position=CGPointMake(w/2, h/2-75);
	replay.position=ccp(replay.position.x-winSize.width/10,replay.position.y);
	quit_button.position=ccp(quit_button.position.x+winSize.width/10,quit_button.position.y);
	[self addChild:cmn z:2 tag:1];
	
	
	///////右下角的config按钮
	/*
	CCMenuItemImage* Menu = [CCMenuItemImage itemFromNormalImage:@"button_expand@1x.png" 
												   selectedImage:@"button_expand_lit@1x.png" 
														  target:self selector:@selector(menuGame:)];
	
	*/
	
	
}

-(void)quitCallback:(id)sender
{
	[GameMusicSetting playEffect:@"button.caf"];
	
    [GameMusicSetting resumeBackgroundMusic];
    
	CCScene *scene = [CCScene node];
	
	CCLayer *layer = [Layer1 node];
	[scene addChild: layer z:0];
	
	//[[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
	[[CCDirector sharedDirector] replaceScene: scene];
}


-(void)enableTouch:(id)sender
{
	//让屏幕支持触摸
	self.isTouchEnabled=YES;
}

-(void)replayGame:(id)sender
{
    [GameMusicSetting resumeBackgroundMusic];
    
	CCScene *sc = [CCScene node];
	[sc addChild:[GameScene scene]];
	[[CCDirector sharedDirector] replaceScene: sc];
}

-(void)menuGame:(id)sender
{

	//[[CCDirector sharedDirector] replaceScene: [CCSlideInRTransition transitionWithDuration:1.2f scene:sc]];
	
	CCScene *scene = [CCScene node];
	
	CCLayer *layer = [Layer1 node];
	[scene addChild: layer z:0];
	
	//[[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
	[[CCDirector sharedDirector] replaceScene: scene];
}




-(void)set_play_score:(int)sco
{
	play_score=sco;
	
	NSLog(@"set_play_score=%d------------",play_score);
}


-(void)set_level:(int)lev
{
	level=lev;
	NSLog(@"after set_level,currnt level=%d------------",level);
}

/*
-(void)gameOverDone
{
	[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
}
*/

-(void)dealloc
{
	[_label release];
	_label=nil;
	
	[super dealloc];
}


#pragma mark ----ad start------
#ifdef SHOW_AD
- (void)onEnter
{
	[super onEnter];
	
	adController = [[AdViewController alloc] initWithMasterView:[[CCDirector sharedDirector] openGLView]];
	[adController setDelegate:self];
	
}

- (void)onExit
{
	//Completely remove the controller
	[adController setDelegate:nil];
	[adController release];
	adController = nil;
	
	[super onExit];
}

- (void)adController:(AdViewController*)controller didLoadiAd:(id)iadBanner;
{
	//Do something when the ad loads, like moving objects.
}

- (void)adController:(AdViewController*)controller didFailedToRecieveiAd:(id)iadBanner;
{
	//Do something when the ad fails to load, like moving objects.;
}

- (void)adController:(AdViewController*)controller didLoadAdMobAd:(AdMobView*)adMobBanner;
{
	//Do something when the ad loads, like moving objects.
}

- (void)adController:(AdViewController*)controller didFailedToRecieveAdMobAd:(AdMobView*)adMobBanner;
{
	//Do something when the ad fails to load, like moving objects.
}
#endif//show ad
#pragma mark ----ad end------




@end


